import request from '../request-axios.js';
import { API_CONFIG } from '../config.js';

// 视频流管理API - 根据API文档更新
export const videoStreamAPI = {
  // 新增视频流信息
  addVideoStream: (data) => {
    console.log('API模块接收到的数据:', data);
    
    const params = {
      streamTitle: data.streamTitle,
      previewUrl: data.previewUrl,
      captureUrl: data.captureUrl,
      streamSort: data.streamSort,
      status: data.status, // 直接使用传入的状态值
      remark: data.remark || ''
    };
    
    console.log('API模块发送的参数:', params);
    console.log('API端点:', API_CONFIG.ENDPOINTS.VIDEO_STREAM_ADD);
    
    return request.post(API_CONFIG.ENDPOINTS.VIDEO_STREAM_ADD, null, { params });
  },

  // 查询视频流信息列表
  getVideoStreams: (params = {}) => {
    return request.post(API_CONFIG.ENDPOINTS.VIDEO_STREAM_LIST, null, { params });
  },

  // 修改视频流信息
  updateVideoStream: (streamId, data) => {
    const params = {
      streamId: streamId,
      streamTitle: data.streamTitle,
      previewUrl: data.previewUrl,
      captureUrl: data.captureUrl,
      streamSort: data.streamSort,
      status: data.status, // 直接使用传入的状态值
      remark: data.remark || ''
    };
    return request.post(API_CONFIG.ENDPOINTS.VIDEO_STREAM_UPDATE, null, { params });
  },

  // 删除视频流信息（支持单个ID或ID数组）
  deleteVideoStream: (idOrIds) => {
    // 统一处理单个ID和ID数组
    let idsParam;
    if (Array.isArray(idOrIds)) {
      // 批量删除：数组转换为逗号拼接字符串
      idsParam = idOrIds.join(',');
    } else {
      // 单个删除：直接使用ID
      idsParam = idOrIds;
    }
    return request.post(API_CONFIG.ENDPOINTS.VIDEO_STREAM_DELETE, null, { params: { ids: idsParam } });
  },

  // 批量删除视频流（兼容性方法，实际调用deleteVideoStream）
  batchDeleteVideoStreams: (ids) => {
    return videoStreamAPI.deleteVideoStream(ids);
  },



  // 获取视频流详情
  getVideoStreamDetail: (streamId) => {
    return request.post(API_CONFIG.ENDPOINTS.VIDEO_STREAM_LIST, null, { params: { streamId } });
  },

  // 启用/禁用视频流
  toggleVideoStream: (streamId, enabled) => {
    const status = enabled ? '0' : '1'; // 0正常 1停用
    return request.post(API_CONFIG.ENDPOINTS.VIDEO_STREAM_UPDATE, null, { 
      params: { 
        streamId, 
        status 
      }
    });
  },

  // 验证视频流链接
  validateVideoStreamUrl: (url) => {
    return request.post(`${API_CONFIG.ENDPOINTS.VIDEO_STREAM_LIST}/validate`, null, { params: { url } });
  }
};

export default videoStreamAPI; 